var arr = [];//存所有数据
var count = 8;//一页多少条数据
var page = 1;//当前的页数
var n;
var user = JSON.parse(localStorage.getItem('user'));
// 判断进入方式
if (localStorage.user) {
    // 插入

    function rem() {
        $('.motaikuang table tbody').html('');
        $.each(arr.slice((page - 1) * count, page * count), function (i, v) {
            var a = v.type == '1' ? '普通管理员' : "超级管理员";
            $('.motaikuang table tbody').append('<tr data-userId="' + v.userId + '">\
    <td class="th1">'+ v.id + '</td>\
    <td>'+ v.title + '</td>\
    <td>'+ v.content + '</td>\
    <td>'+ v.mytime + '</td>\
    <td>'+ v.name + '</td>\
    <td>\
            <button type="button" class="shu" data-toggle="modal" data-target="#myModal5">\
                删除\
            </button>\
            <button type="button" class="shu"\ data-toggle="modal"data-target="#exampleModal5"\
                data-whatever="@mdo">修改</button>\
    </td>\
</tr>'
            )
        })
    }
    function rwt() {
        $.ajax({
            url: '/api/notice/getnotice',
            success: function (res) {
                console.log(res)
                if (res.data.length) {
                    arr = res.data;
                    rem();
                    ret();
                }

            },
        })
    }
    rwt();
    // 页码的点击
    $('#nav2').on('click', 'li', function () {
        page = $(this).text();
        rem();
    })
    function ret() {
        var n = Math.ceil(arr.length / count);
        $('.diyebiao').remove();
        for (var i = 1; i <= n; i++) {
            $('.yeshu').before($('<li class="diyebiao"><a href="#">' + i + '</a></li>'));
        }
    }
    // 添加
    $('.j-add-btn').on('click', function () {
        // console.log(11);
        $.ajax({
            url: '/api/notice/addnotice',
            data: {
                content: $('.j-add-content').val(),
                title: $('.j-add-title').val(),
                userId: 3,
            },

            type: 'post',
            success: function (res) {
                // console.log(res)
                if (res.code == 200) {
                    $('#exampleModal1').modal('hide');
                    rwt();
                } else {
                    alert('添加失败')
                }
            }
        })
    })
    console.log($('.j-add-content').val())
    //删除
    // 当点击确定删除按钮的时候
    $('.gonggaoshanchu').on('click', function () {
        $.ajax({
            url: '/api/notice/delnotice',
            data: {
                id: n
            },
            type: 'post',
            success: function (res) {
                // console.log(res)
                if (res.code == 200) {
                    $('#myModal5').modal('hide');
                    rwt();
                } else {
                    alert('删除失败')
                }
            }
        })
    })
    // 点击删除的时候
    $('#myModal5').on('show.bs.modal', function (e) {
        n = $(e.relatedTarget).parents('tr').children().eq(0).text();

    })
    //修改
    // 当点击修改的时候
    $('#exampleModal5').on('show.bs.modal', function (e) {
        var tr = $(e.relatedTarget).parents('tr');
        $('.j-s-bian').val(tr.children().eq(0).text());
        $('.j-s-biao').val(tr.children().eq(1).text());
        $('.j-s-nei').val(tr.children().eq(2).text());
        $('.j-s-shi').val(tr.children().eq(3).text());
        $('.j-s-ren').val(tr.children().eq(4).text());
    })
    // 当点击确定的时候
    $('.j-s-q').on('click', function () {

        $.ajax({
            url: '/api/notice/updatenotice',
            data: {
                content: $('.j-s-nei').val(),
                title: $('.j-s-biao').val(),
                id: $('.j-s-bian').val(),

            },
            type: 'post',
            success: function (res) {
                if (res.code == 200) {
                    $('#exampleModal5').modal('hide');
                    rwt();
                } else {
                    alert("修改失败")
                }
            }
        })
    })

    //切换账号
    $('#qiehuanzhanghao').on('click', function () {
        window.location.href = '/wb258-2/page/HomePage.html'
    })
    // 退出
    $('#guanbichuangko').on('click', function () {
        closePage();
        // window.location.href="https://blog.csdn.net/kuangfengbuyi/article/details/52052301"

    })
    function closePage() {
        if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1) {
            window.location.href = "about:blank";
            window.close();
        } else {
            window.opener = null;
            window.open("", "_self");
            window.close();
        }
    }
} else {
    window.location.href = '/wb258-2/page/HomePage.html'
}